﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using Microsoft.Practices.ServiceLocation;

#if !WINRT
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
#else
using Windows.ApplicationModel;
#endif

namespace MishraReader
{
    public static class ServiceLocator
    {
        private static readonly Lazy<IServiceLocator> CurrentLocator;

        private static readonly Lazy<bool> _isInDesignMode;

        static ServiceLocator()
        {
            _isInDesignMode = new Lazy<bool>(
                () =>
                    {
#if !WINRT
                        var isInDesignMode = ViewModelBase.IsInDesignModeStatic;
                        //if (!isInDesignMode && (
                        //                           Process.GetCurrentProcess().ProcessName.StartsWith("devenv",
                        //                                                                              StringComparison.
                        //                                                                                  OrdinalIgnoreCase) ||
                        //                           Process.GetCurrentProcess().ProcessName.StartsWith("xdesproc",
                        //                                                                              StringComparison.
                        //                                                                                  OrdinalIgnoreCase))
                        //    )

                        //{
                        //    isInDesignMode = true;
                        //}

                        return isInDesignMode;
#else
                        return DesignMode.DesignModeEnabled;
#endif

                    });

            CurrentLocator =
                new Lazy<IServiceLocator>(() => IsInDesignMode ? new DesignTimeServiceLocator() : Microsoft.Practices.ServiceLocation.ServiceLocator.Current);
        }

        public static IServiceLocator Current
        {
            get { return CurrentLocator.Value; }
        }

        public static bool IsInDesignMode
        {
            get { return _isInDesignMode.Value; }
        }

        private class DesignTimeServiceLocator : IServiceLocator
        {
            public IEnumerable<object> GetAllInstances(Type serviceType)
            {
                return Enumerable.Empty<object>();
            }

            public IEnumerable<TService> GetAllInstances<TService>()
            {
                return Enumerable.Empty<TService>();
            }

            public object GetInstance(Type serviceType)
            {
                return null;
            }

            public object GetInstance(Type serviceType, string key)
            {
                return null;
            }

            public TService GetInstance<TService>()
            {
                return default(TService);
            }

            public TService GetInstance<TService>(string key)
            {
                return default(TService);
            }

            public object GetService(Type serviceType)
            {
                return null;
            }
        }
    }
}